import json
import time
import requests
with open("weblog.txt","r+") as fp:
    for i in fp:
        line=i.split()
        ip=line[0]
        file_time=line[3][1:]
        Bandwidth=line[9]
        # strptime( )函数用于根据format的格式把一个时间字符串解析为时间元组。
        file_time = time.strptime(file_time, "%d/%b/%Y:%H:%M:%S")
        # mktime接收 struct_time 对象作为参数，返回用秒数表示时间的浮点数。如果输入的值不是合法时间，就会触发OverflowError或ValueErro
        file_time=time.mktime(file_time)
        # strftime()  方法用于接收时间元组，并返回以可读字符串表示的当地时间。格式由format参数决定。
        file_time=time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(file_time))
        response=requests.get(f' https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip}')
        response=response.json()
        province=(response.get("data")).get("region")
        isp=(response.get("data")).get("isp")
        # province=province.get("region")
        # print(response)
        # print(f"{ip},{file_time},{Bandwidth}")
        print(f"{file_time},{province},{isp},{Bandwidth}")
        with open("result.txt","a+",encoding="utf-8") as fp1:
            resul=f"{file_time},{province},{isp},{Bandwidth}"+"\n"
            fp1.write(resul)


